# 6. 模型评估
def evaluate_model(model, X_test, y_test, X_train=None, y_train=None):
    """
    评估模型性能
    """
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.metrics import (accuracy_score, precision_score, recall_score, f1_score,roc_auc_score,
                                 classification_report)
    # import seaborn as sns

    print("\n=== 模型评估 ===\n")

    # 6.1 在测试集上进行预测
    y_pred = model.predict(X_test)

    # 6.2 计算评估指标（记得使用predict_proba）
    accuracy = accuracy_score(y_test, y_pred)
    precision = precision_score(y_test, y_pred)
    recall = recall_score(y_test, y_pred)
    f1 = f1_score(y_test, y_pred)
    auc_score =  roc_auc_score(y_test, y_pred)

    print("测试集性能指标:")
    print(f"准确率 (Accuracy): {accuracy:.4f}")
    print(f"精确率 (Precision): {precision:.4f}")
    print(f"召回率 (Recall): {recall:.4f}")
    print(f"F1 分数: {f1:.4f}")
    print(f"AUC 值: {auc_score:.4f}")

    # 如果提供了训练集，评估过拟合情况
    if X_train is not None and y_train is not None:
        train_pred = model.predict(X_train)
        train_accuracy = accuracy_score(y_train, train_pred)
        print(f"\n训练集准确率: {train_accuracy:.4f}")
        print(f"测试集准确率: {accuracy:.4f}")
        if train_accuracy - accuracy > 0.1:
            print("⚠️ 模型可能存在过拟合")
        else:
            print("✅ 模型泛化能力良好")

    # 可选：返回指标字典
    metrics = {
        'accuracy': accuracy,
        'precision': precision,
        'recall': recall,
        'f1': f1,
        'auc': auc_score
    }

    return metrics
if __name__ == '__main__':
    pass
    # XGBoost模型预测评估
    # best_xgb_model
    # X_test
    # y_test
    # X_train
    # y_train
    # xgb_evaluate=evaluate_model(model, X_test, y_test, X_train=None, y_train=None)
    # LightGBM模型预测评估
    # best_lgb_model
    # X_test
    # y_test
    # X_train
    # y_train
    # lgb_evaluate = evaluate_model(model, X_test, y_test, X_train=None, y_train=None)